var NODE_MAP = new Map_();

function createNode(jsonNode, scene) {
    var node = new JTopo.Node(jsonNode.nodeName);
    node.setSize(jsonNode.fontSizeX, jsonNode.fontSizeY);
    node.setLocation(jsonNode.x, jsonNode.y);
    node.setImage(jsonNode.nodeImage, false);
    node.fontColor = '20,10,20';
    if (jsonNode.alarmText != undefined && jsonNode.alarmText != "" && jsonNode.alarmText != "undefined") {
        node.alarm = jsonNode.alarmText;
    }
    scene.add(node);
    initARightButton(node);
    NODE_MAP.put(jsonNode.nodeId, node);
}

function createLine(jsonNode, scene) {
    var fromNode = NODE_MAP.get(jsonNode.fromNodeId);
    var toNode = NODE_MAP.get(jsonNode.toNodeId);
    var link = new JTopo.Link(fromNode, toNode);
    link.shadow = false;
    if (jsonNode.strokeColor && jsonNode.strokeColor != "") {
        link.strokeColor = jsonNode.strokeColor;
    }
    if (jsonNode.dashedPattern && jsonNode.dashedPattern != "") {
    	link.dashedPattern = jsonNode.dashedPattern;
    	link.bundleGap = 20; // 线条之间的间隔
    }
    if (jsonNode.strokeColor && jsonNode.strokeColor != "") {
    	link.lineWidth = jsonNode.lineWidth;
    }else{
    	link.lineWidth = 1.5;
    }
    initARightButton(link);
    scene.add(link);
}

function createFoldLink(jsonNode,scene){
	var fromNode = NODE_MAP.get(jsonNode.fromNodeId);
	var toNode = NODE_MAP.get(jsonNode.toNodeId);
	var link = new JTopo.FoldLink(fromNode, toNode);
    link.direction = direction || 'horizontal';
    link.arrowsRadius = 15; //箭头大小
    link.lineWidth = 3; // 线宽
    link.bundleOffset = 60; // 折线拐角处的长度
    link.bundleGap = 20; // 线条之间的间隔
    link.textOffsetY = 3; // 文本偏移量（向下3个像素）
    link.strokeColor = JTopo.util.randomColor(); // 线条颜色随机
    link.dashedPattern = dashedPattern;
    scene.add(link);
    initARightButton(link);
    return link;
}

function createTextNode(jsonNode, scene) {
    var node = new JTopo.TextNode(jsonNode.nodeName);
    node.setLocation(jsonNode.x, jsonNode.y);
    node.fontColor = '20,10,20';
    node._id = Math.uuid();
    initARightButton(node);
    scene.add(node);
    NODE_MAP.put(jsonNode.nodeId, node);
}

function renderToPo(jsons) {
    scene.clear();
    for (var i = 0; i < jsons.length; i++) {
        var jsonNode = jsons[i];
        if (jsonNode.nodeType == "node") {
            createNode(jsonNode, scene);
        } else if (jsonNode.nodeType == "link") {
            createLine(jsonNode, scene);
        } else if (jsonNode.nodeType == 'TextNode') {
            createTextNode(jsonNode, scene);
        }else if (jsonNode.nodeType == 'FoldLink') {
        	createFoldLink(jsonNode, scene);
        }
    }
}

function saveJson() {
    var k = scene.getDisplayedElements();
    var kLength = k.length;
    var sb = "[";
    for (var i = kLength - 1; i >= 0; i--) {
        var node = k[i];
        if (node != undefined) {
            var subNodeImage = "";
            try {
                var nodeImage = node['image'] && node['image']['src'];
                if (nodeImage != undefined) {
                    subNodeImage = nodeImage.substr(nodeImage.lastIndexOf("/doroodo"), nodeImage.length);
                } else {
                    subNodeImage = "";
                }
            } catch (e) {
                subNodeImage = "";
            }
            node._id = Math.uuid();
            if (node.elementType == 'node' || node.elementType == 'TextNode') {
                sb = sb + '{nodeType:"' + node.elementType + '",nodeId:"' + node._id + '",nodeName:"' + node.text + '",x:' + node.x + ',' +
                    'y:' + node.y + ',fromNodeId:-1,toNodeId:-1,nodeImage:"' + subNodeImage + '",alarmText:"' + node.alarm + '"' +
                    ',fontSizeX:' + node.width + ',fontSizeY:' + node.height + ',nodeField:"'+node.field+'",elementEvent:"'+node.elementEvent+'",strokeColor:""}\r\n'
                if (i != 0) {
                    sb = sb + ",";
                }
            } else if (node.elementType == 'link') {
                if (node.nodeA != null && node.nodeZ != null) {
                    sb = sb + '{nodeType:"' + node.elementType + '",nodeId:"' + node._id + '",nodeName:"' + node.text + '",x:' + node.x + ',' +
                        'y:' + node.y + ',fromNodeId:"' + node.nodeA._id + '",toNodeId:"' + node.nodeZ._id + '",nodeImage: "",alarmText:"' + node.alarm +
                        '",fontSizeX:' + node.width + ',fontSizeY:' + node.height + ',strokeColor:"' + node.strokeColor + '",lineWidth:'+node.lineWidth+',dashedPattern:'+node.dashedPattern+'}\r\n';
                    if (i != 0) {
                        sb = sb + ",";
                    }
                }
            }
        }
    }
    sb = sb + "]";
    if (sb.substr(sb.length - 2, sb.length) == ",]") {
        sb = sb.substr(0, sb.length - 3) + "]";
    }
    parent.document.getElementById("theJson").value = sb;
}